<?php
namespace app\home\controller;

use app\home\model\Student as StudentModel;
use \think\Session as Session;
use \think\Validate;
class Student extends Home{

    /**
     * 登录输入页；
     * 
     * @return html
     * @date Tue Jun 26 12:19:00 2018
     */
    public function login(){
        return $this->fetch();
    }

    /**
     * 登录验证页
     * 
     * @return 
     * @date Tue Jun 26 12:19:29 2018
     */
    public function enter(){
        $data = $this->req->post();
        $model = new StudentModel;
        $info = $model->where(['stu_login'=>$data['login'],'stu_password'=>$data['pwd']])->find();
        if($info){
            Session::set('stu.name',$info['stu_name']);
            Session::set('stu.id',$info['stu_id']);
            $this->redirect("person");
        }else{
            $this->error();
        }
        
    }

    public function person(){
        return $this->fetch();
    }

    /**
     * 查看课程
     * 
     * @return 
     * @date Wed Jul 11 11:56:46 2018
     */
    public function Course(){
        if(Session::has('stu')){
            $stuid = Session::get('stu.id');
            $model = new \app\home\model\StudentCourse;
            $data = $model->where(['stu_id'=>$stuid])->select();
            if($data){
                $this->assign('data',$data);
                $info = [
                    'msg' => '获取数据成功',
                    'data' => $this->fetch()
                ];

                echo $this->right($info);
            }else{
                $info = [
                    'msg' => '获取数据失败',
                ];

                echo $this->wrong($info);
                
            }
        }
        return;
    }

    /**
     * 学员查看自己的留言
     * 
     * @return ajax html / error
     * @date Mon Jun 18 19:43:16 2018
     */
    public function comment(){
        if(Session::has('stu')){
            $stuid = Session::get('stu.id');
            $model = new \app\home\model\ArticleComment;
            $c_data = $model->alias('c')->join('edu_article a','a.art_id = c.art_id')->where(['stu_id'=>$stuid])->select();
            $model = new \app\home\model\StudentComment;
            $s_data = $model->alias('c')->join('edu_book_chapter ch','ch.chat_id = c.chat_id')->where(['stu_id'=>$stuid])->select();
            if($c_data || $s_data){
                $this->assign('cdata',$c_data);
                $this->assign('sdata',$s_data);
                $info = [
                    'msg' => '获取数据成功',
                    'data' => $this->fetch()
                ];

                echo $this->right($info);
            }else{
                $info = [
                    'msg' => '获取数据失败',
                ];

                echo $this->wrong($info);
                
            }            
        }else{
                $info = [
                    'msg' => '获取数据失败',
                ];

                echo $this->wrong($info);
            
        }
    }

    /**
     * 试卷信息页面
     * 
     * @return 
     * @date Sun Jun 24 17:23:34 2018
     */
    public function paper(){
        $model = new \app\home\model\Paper;
        $choice = $model->getchoice();
        $essay = $model->getessay();
        if($choice || $essay){
            $this->assign('choice',$choice);
            $this->assign('essay',$essay);
            $info = [
                'msg' => '获取数据成功',
                'data' => $this->fetch()
            ];
            echo $this->right($info);
        }else{
            $info = [
                'msg' => '获取数据失败',
            ];
            echo $this->wrong($info);
        }
        return;
    }

    /**
     * 对老师的提问
     * 
     * @return html
     * @date Tue Jun 26 21:19:10 2018
     */
    public function ask(){
        $info = [
            'msg' => '获取数据成功',
            'data' => $this->fetch()
        ];
        echo $this->right($info);

    }

    /**
     * 练习历史查看
     * 
     * @return html
     * @date Tue Jun 26 21:18:40 2018
     */
    public function test(){
        if(Session::has('stu')){
            $stuid = Session::get('stu.id');
            $model = new \app\home\model\Test;
            $data = $model->examed($stuid);
            if($data){
                $this->assign('data',$data);
                $info = [
                    'msg' => '获取数据成功',
                    'data' => $this->fetch()
                ];
                echo $this->right($info);

            }else{
                $info = [
                    'msg' => '获取数据失败',
                ];

                echo $this->wrong($info);
                
            }            
        }else{
            $info = [
                'msg' => '获取数据失败',
            ];

            echo $this->wrong($info);
            
        }
           
    }


    public function art(){
        $artmodel = new \app\home\model\Article;
        $data = $artmodel->field('art_title,art_content')->where(['art_cat_id'=>2])->select();
        foreach($data as $v){
            $chatmodel = new \app\admin\model\Chapter;

            echo "1";
            $d['chat_title'] = $v['art_title'];
            $d['chat_content'] = $v['art_content'];
            $d['chat_exercise'] = '';
            $d['chat_sort'] = 10;
            $chatmodel->save($d);
        }
    }

    public function project(){
        $model = new \app\home\model\StudentProject;
        $data = $model->select();
        if($data){
            $this->assign('data',$data);
            $info = [
                'data' => $this->fetch(),
                'msg' => '读取数据成功'
            ];
            echo $this->right($info);
        }else{
            $info = [
                'msg' => '系统出错'
            ];
            echo $this->wrong($info);
        }
        return;
    }

    public function detail_project(){
        $get = $this->req->get();
        $rule = [
            'id'=>'require'
        ];
        $validate = new Validate($rule);
        if($validate->check($get)){
            $model = new \app\home\model\StudentProject;
            $project = $model->where(['pro_id'=>$get['id']])->find();
            if($project){
                $this->assign('project',$project);
                return $this->fetch();
            }else{
                return  $this->error('暂时没有项目');
            }
        }
        return;
    }
}
